package com.bot.edu.noticessystem.notices.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bot.edu.noticessystem.announcement.entity.Announcement;
import com.bot.edu.noticessystem.announcement.service.IAnnouncementService;
import com.bot.edu.utils.SnowFlake;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.bot.edu.noticessystem.notices.entity.Notices;
import com.bot.edu.noticessystem.notices.service.INoticesService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 通知表
 * @Author: jeecg-boot
 * @Date:   2022-04-27
 * @Version: V1.0
 */
@Api(tags="通知表")
@RestController
@RequestMapping("/notices/notices")
@Slf4j
public class NoticesController extends JeecgController<Notices, INoticesService> {
	@Autowired
	private INoticesService noticesService;

	@Autowired
    private IAnnouncementService announcementService;
	
	/**
	 * 分页列表查询
	 *
	 * @param notices
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "通知表-分页列表查询")
	@ApiOperation(value="通知表-分页列表查询", notes="通知表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Notices>> queryPageList(Notices notices,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Notices> queryWrapper = QueryGenerator.initQueryWrapper(notices, req.getParameterMap());
		Page<Notices> page = new Page<Notices>(pageNo, pageSize);
		IPage<Notices> pageList = noticesService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param notices
	 * @return
	 */
	@AutoLog(value = "通知表-添加")
	@ApiOperation(value="通知表-添加", notes="通知表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Notices notices) {
	    notices.setNoticesId(SnowFlake.getId());
		noticesService.save(notices);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param notices
	 * @return
	 */
	@AutoLog(value = "通知表-编辑")
	@ApiOperation(value="通知表-编辑", notes="通知表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Notices notices) {
		noticesService.updateById(notices);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "通知表-通过id删除")
	@ApiOperation(value="通知表-通过id删除", notes="通知表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		noticesService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "通知表-批量删除")
	@ApiOperation(value="通知表-批量删除", notes="通知表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.noticesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "通知表-通过id查询")
	@ApiOperation(value="通知表-通过id查询", notes="通知表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Notices> queryById(@RequestParam(name="id",required=true) String id) {
		Notices notices = noticesService.getById(id);
		if(notices==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(notices);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param notices
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Notices notices) {
        return super.exportXls(request, notices, Notices.class, "通知表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Notices.class);
    }

     /**
      * 根据id查看消息
      * @param noticeNameId
      * @param noticesType
      * @return
      */
    @GetMapping("/getNotices")
     public Result<?> getNotices(String noticeNameId,String noticesType){
        QueryWrapper<Notices> noticesQueryWrapper = new QueryWrapper<>();
        noticesQueryWrapper.lambda().eq(Notices::getNoticeNameId,noticeNameId).or().eq(Notices::getNoticeNameId,noticesType).or().eq(Notices::getNoticeNameId,"666");
        List<Notices> noticesList = noticesService.list(noticesQueryWrapper);
        QueryWrapper<Announcement> announcementQueryWrapper = new QueryWrapper<>();
        announcementQueryWrapper.lambda().eq(Announcement::getNoticeNameId,noticeNameId);
        List<Announcement> announcementList = announcementService.list(announcementQueryWrapper);
        for (int i = 0; i < noticesList.size(); i++) {
            for (int j = 0; j < announcementList.size(); j++) {
                if (noticesList.get(i).getNoticesId().equals(announcementList.get(j).getNoticesId())) {
                    noticesList.get(i).setNoticeState("1");
                    if ("0".equals(announcementList.get(j).getNoticeState())){
                        noticesList.remove(i);
                    }
                }
            }
        }
        return Result.ok(noticesList);
    }

     /**
      * 查看未读消息个数
      * @param noticeNameId
      * @param noticesType
      * @return
      */
     @GetMapping("/noticesCount")
     public Result<?> noticesCount(String noticeNameId,String noticesType){
         QueryWrapper<Notices> noticesQueryWrapper = new QueryWrapper<>();
         noticesQueryWrapper.lambda().eq(Notices::getNoticeState,0);
         noticesQueryWrapper.lambda().eq(Notices::getNoticeNameId,noticeNameId).or().eq(Notices::getNoticeNameId,noticesType).or().eq(Notices::getNoticeNameId,"666");
         int noticesCount = noticesService.count(noticesQueryWrapper);
         QueryWrapper<Announcement> announcementQueryWrapper = new QueryWrapper<>();
         announcementQueryWrapper.lambda().eq(Announcement::getNoticeNameId,noticeNameId).eq(Announcement::getNoticeState,1);
         int announcementCount = announcementService.count(announcementQueryWrapper);
         int count = noticesCount-announcementCount;
         /*int noticesCount = noticesService.noticesCount(noticeNameId,noticesType);*/
         return Result.ok(count);
     }
}
